﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IPC.Communication.Core;

namespace IPC.Communication.Profinet.Inovance
{
    public class InovanceHelper
    {
        private static int CalculateStartAddress(string address)
        {
            if (address.IndexOf('.') < 0)
            {
                return int.Parse(address);
            }
            string[] array = address.Split(new char[1]
		{
			'.'
		}, StringSplitOptions.RemoveEmptyEntries);
            return int.Parse(array[0]) * 8 + int.Parse(array[1]);
        }

        public static OperateResult<string> PraseInovanceAddress(InovanceSeries series, string address, byte modbusCode)
        {
            switch (series)
            {
                case InovanceSeries.AM:
                    return PraseInovanceAMAddress(address, modbusCode);
                case InovanceSeries.H3U:
                    return PraseInovanceH3UAddress(address, modbusCode);
                case InovanceSeries.H5U:
                    return PraseInovanceH5UAddress(address, modbusCode);
                default:
                    return new OperateResult<string>(String.Format("[{0}] Not supported series of plc", series));
            }
        }

        public static OperateResult<string> PraseInovanceAMAddress(string address, byte modbusCode)
        {
            try
            {
                string str = string.Empty;
                OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
                if (operateResult.IsSuccess)
                {
                    str = String.Format("s={0};", operateResult.Content);
                }
                if (address.StartsWith("QX") || address.StartsWith("qx"))
                {
                    return OperateResult.CreateSuccessResult(str + CalculateStartAddress(address.Substring(2)).ToString());
                }
                if (address.StartsWith("Q") || address.StartsWith("q"))
                {
                    return OperateResult.CreateSuccessResult(str + CalculateStartAddress(address.Substring(1)).ToString());
                }
                if (address.StartsWith("IX") || address.StartsWith("ix"))
                {
                    return OperateResult.CreateSuccessResult(str + "x=2;" + CalculateStartAddress(address.Substring(2)).ToString());
                }
                if (address.StartsWith("I") || address.StartsWith("i"))
                {
                    return OperateResult.CreateSuccessResult(str + "x=2;" + CalculateStartAddress(address.Substring(1)).ToString());
                }
                if (address.StartsWith("MW") || address.StartsWith("mw"))
                {
                    return OperateResult.CreateSuccessResult(str + address.Substring(2));
                }
                if (address.StartsWith("M") || address.StartsWith("m"))
                {
                    return OperateResult.CreateSuccessResult(str + address.Substring(1));
                }
                if (modbusCode == 1 || modbusCode == 15 || modbusCode == 5)
                {
                    if (address.StartsWith("SMX") || address.StartsWith("smx"))
                    {
                        return OperateResult.CreateSuccessResult(str + String.Format("x={0};", modbusCode + 48) + CalculateStartAddress(address.Substring(3)).ToString());
                    }
                    if (address.StartsWith("SM") || address.StartsWith("sm"))
                    {
                        return OperateResult.CreateSuccessResult(str + String.Format("x={0};", modbusCode + 48) + CalculateStartAddress(address.Substring(2)).ToString());
                    }
                }
                else
                {
                    if (address.StartsWith("SDW") || address.StartsWith("sdw"))
                    {
                        return OperateResult.CreateSuccessResult(str + String.Format("x={0};", modbusCode + 48) + address.Substring(3));
                    }
                    if (address.StartsWith("SD") || address.StartsWith("sd"))
                    {
                        return OperateResult.CreateSuccessResult(str + String.Format("x={0};", modbusCode + 48) + address.Substring(2));
                    }
                }
                return new OperateResult<string>(StringResources.Language.NotSupportedDataType);
            }
            catch (Exception ex)
            {
                return new OperateResult<string>(ex.Message);
            }
        }

        private static int CalculateH3UStartAddress(string address)
        {
            if (address.IndexOf('.') < 0)
            {
                return Convert.ToInt32(address, 8);
            }
            string[] array = address.Split(new char[1]
		{
			'.'
		}, StringSplitOptions.RemoveEmptyEntries);
            return Convert.ToInt32(array[0], 8) * 8 + int.Parse(array[1]);
        }

        public static OperateResult<string> PraseInovanceH3UAddress(string address, byte modbusCode)
        {
            try
            {
                string str = string.Empty;
                OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
                if (operateResult.IsSuccess)
                {
                    str = String.Format("s={0};", operateResult.Content);
                }
                if (modbusCode == 1 || modbusCode == 15 || modbusCode == 5)
                {
                    if (address.StartsWith("X") || address.StartsWith("x"))
                    {
                        return OperateResult.CreateSuccessResult(str + (CalculateH3UStartAddress(address.Substring(1)) + 63488).ToString());
                    }
                    if (address.StartsWith("Y") || address.StartsWith("y"))
                    {
                        return OperateResult.CreateSuccessResult(str + (CalculateH3UStartAddress(address.Substring(1)) + 64512).ToString());
                    }
                    if (address.StartsWith("SM") || address.StartsWith("sm"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(2)) + 9216).ToString());
                    }
                    if (address.StartsWith("S") || address.StartsWith("s"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 57344).ToString());
                    }
                    if (address.StartsWith("T") || address.StartsWith("t"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 61440).ToString());
                    }
                    if (address.StartsWith("C") || address.StartsWith("c"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 62464).ToString());
                    }
                    if (address.StartsWith("M") || address.StartsWith("m"))
                    {
                        int num = Convert.ToInt32(address.Substring(1));
                        if (num >= 8000)
                        {
                            return OperateResult.CreateSuccessResult(str + (num - 8000 + 8000).ToString());
                        }
                        return OperateResult.CreateSuccessResult(str + num.ToString());
                    }
                }
                else
                {
                    if (address.StartsWith("D") || address.StartsWith("d"))
                    {
                        return OperateResult.CreateSuccessResult(str + Convert.ToInt32(address.Substring(1)).ToString());
                    }
                    if (address.StartsWith("SD") || address.StartsWith("sd"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(2)) + 9216).ToString());
                    }
                    if (address.StartsWith("R") || address.StartsWith("r"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
                    }
                    if (address.StartsWith("T") || address.StartsWith("t"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 61440).ToString());
                    }
                    if (address.StartsWith("C") || address.StartsWith("c"))
                    {
                        int num2 = Convert.ToInt32(address.Substring(1));
                        if (num2 >= 200)
                        {
                            return OperateResult.CreateSuccessResult(str + ((num2 - 200) * 2 + 63232).ToString());
                        }
                        return OperateResult.CreateSuccessResult(str + (num2 + 62464).ToString());
                    }
                }
                return new OperateResult<string>(StringResources.Language.NotSupportedDataType);
            }
            catch (Exception ex)
            {
                return new OperateResult<string>(ex.Message);
            }
        }

        public static OperateResult<string> PraseInovanceH5UAddress(string address, byte modbusCode)
        {
            try
            {
                string str = string.Empty;
                OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
                if (operateResult.IsSuccess)
                {
                    str = String.Format("s={0};", operateResult.Content);
                }
                if (modbusCode == 1 || modbusCode == 15 || modbusCode == 5)
                {
                    if (address.StartsWith("X") || address.StartsWith("x"))
                    {
                        return OperateResult.CreateSuccessResult(str + (CalculateH3UStartAddress(address.Substring(1)) + 63488).ToString());
                    }
                    if (address.StartsWith("Y") || address.StartsWith("y"))
                    {
                        return OperateResult.CreateSuccessResult(str + (CalculateH3UStartAddress(address.Substring(1)) + 64512).ToString());
                    }
                    if (address.StartsWith("S") || address.StartsWith("s"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 57344).ToString());
                    }
                    if (address.StartsWith("B") || address.StartsWith("b"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
                    }
                    if (address.StartsWith("M") || address.StartsWith("m"))
                    {
                        return OperateResult.CreateSuccessResult(str + Convert.ToInt32(address.Substring(1)).ToString());
                    }
                }
                else
                {
                    if (address.StartsWith("D") || address.StartsWith("d"))
                    {
                        return OperateResult.CreateSuccessResult(str + Convert.ToInt32(address.Substring(1)).ToString());
                    }
                    if (address.StartsWith("R") || address.StartsWith("r"))
                    {
                        return OperateResult.CreateSuccessResult(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
                    }
                }
                return new OperateResult<string>(StringResources.Language.NotSupportedDataType);
            }
            catch (Exception ex)
            {
                return new OperateResult<string>(ex.Message);
            }
        }
    }
}